package com.neu.dao.impl;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
import com.neu.dao.IpatientDao;
import com.neu.pojo.Patient;

public class PatientDao implements IpatientDao {
	//属性
	private File file = new File("patient.json");
	private ObjectMapper om = new ObjectMapper();
	//单例模式
	private static IpatientDao patientDao = new PatientDao();
	private PatientDao() {}
	public static IpatientDao getInstance() {
		return patientDao;
	}
	
	
	//添加病人
	@Override
	public boolean add(Patient patient) {
		ArrayList<Patient> patients = new ArrayList<>();//模板容器
		
		try {
			patients = this.om.readValue(file,new TypeReference<List<Patient>>(){});//反序列化
			for(Patient p:patients) {//遍历
				if(p.getId().equals(patient.getId())) {//找到
					return false;//返回
				}
			}
			patients.add(patient);//添加
			this.om.writeValue(file, patients);//序列化
			return true;//返回
		} catch (MismatchedInputException e) {//文件内无内容
			patients.add(patient);//添加
			try {
				this.om.writeValue(file, patients);//序列化
				return true;//返回
			} catch (IOException e1) {
				e1.printStackTrace();
				return false;//返回
			}
		} catch (IOException e) {//异常
			e.printStackTrace();
		}
		return false;//返回
	}

	//���ڲ�����Ϣ,��¼��
	@Override
	public Patient search(Patient patient) {
		ArrayList<Patient> patients = new ArrayList<>();//模板容器
		
		try {
			patients = this.om.readValue(file,new TypeReference<List<Patient>>(){});//反序列化

			for(Patient p:patients) {//遍历
				if(p.getId().equals(patient.getId())) {//找到了
					return p;//返回
				}
			}
			return null;//返回
		} catch (MismatchedInputException e) {//文件内无内容
			return null;//返回
		} catch (IOException e) {//异常
			e.printStackTrace();
			return null;//返回
		}
	}
	
	//覆盖
	@Override
	public boolean cover(Patient patient) {
		ArrayList<Patient> patients = new ArrayList<>();//模板容器
		
		try {
			patients = this.om.readValue(file,new TypeReference<List<Patient>>(){});//反序列化
			for(int i=0;i<patients.size();i++) {//遍历
				if(patients.get(i).getId().equals(patient.getId())) {
					patients.get(i).setBirthday(patient.getBirthday());
					patients.get(i).setCloseName(patient.getCloseName());
					patients.get(i).setClosePhone(patient.getClosePhone());
					patients.get(i).setName(patient.getName());
					patients.get(i).setPhone(patient.getPhone());
					patients.get(i).setSex(patient.getSex());
					this.om.writeValue(file, patients);//序列化
					return true;//返回
				}
			}
			return false;//返回
		} catch (MismatchedInputException e) {//文件内无内容
			return false;//返回
		} catch (IOException e) {//异常
			e.printStackTrace();
			return false;//返回
		}
	}
	
	//getAll
	@Override
	public ArrayList<Patient> list(){
		ArrayList<Patient> patients = new ArrayList<>();//模板容器
		
		try {
			patients = this.om.readValue(file,new TypeReference<List<Patient>>(){});//反序列化
			return patients;//返回
		}catch (MismatchedInputException e) {//文件内无内容
			return patients;//返回
		}catch (IOException e) {//异常
			e.printStackTrace();
			return null;//返回
		}
	}
	
	@Override
	public boolean delete(Patient p) {
		ArrayList<Patient> patients = new ArrayList<>();//模板容器
		
		try {
			patients = this.om.readValue(file,new TypeReference<List<Patient>>(){});//反序列化
			for(int i=0;i<patients.size();i++) {//遍历
				if(patients.get(i).getId().equals(p.getId())) {
					patients.remove(i);
					this.om.writeValue(file, patients);//序列化
					return true;//返回
				}
			}
			return false;//返回
		}catch (MismatchedInputException e) {//文件内无内容
			return false;//返回
		}catch (IOException e) {//异常
			e.printStackTrace();
			return false;//返回
		}
	}
	
	//测试，加数据
//	public static void main(String[] args) {
//		Patient p = new Patient("1", "1", "1", "1", "1", "1", "1");
//		System.out.println(PatientDao.getInstance().list().size());
//		
//	}
	
}
